package view.dialog;

import javax.imageio.ImageIO;
import javax.swing.*;

import net.miginfocom.swing.MigLayout;

import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * Dialog that displays camera (security stuff).
 *	
 * @author Kenny Chong, Dan End, James Ju
 * CSE 333 : User Interface
 * Project HMSystem
 */
public class CameraViewDialog extends JDialog
{
	private JButton nextButton;
	private JButton previousButton;
	private JButton okayButton;
	
	private JLabel imageLabel;
	private JPanel buttonPanel;
	
	private JLabel cameraLabel;
	
	// Test Images.
	private BufferedImage floorOneImg;
	private BufferedImage floorTwoImg;
	
	private BufferedImage [] imageArray;
	
	private MigLayout layout;
	
	public CameraViewDialog()
	{
		this.setTitle("Security Camera");
		
		// Initialize buttons.
		nextButton = new JButton(new ImageIcon("images/rightarrow.png"));
		nextButton.setToolTipText("To Next Camera Look");
		
		previousButton = new JButton(new ImageIcon("images/leftarrow.png"));
		previousButton.setToolTipText("To Previous Camera Look");
	
		okayButton = new JButton(new ImageIcon("images/check.png"));
		
		layout = new MigLayout();
		
		this.setLayout(layout);
		
		// Attempt to read files.
		try
		{
			floorOneImg = ImageIO.read(new File("images/floor.jpg"));
			floorTwoImg = ImageIO.read(new File("images/floorLobby.jpg"));
			
			imageArray = new BufferedImage[2];
			
			imageArray[0] = floorOneImg;
			imageArray[1] = floorTwoImg;
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		cameraLabel = new JLabel("Camera:");
		imageLabel = new JLabel(new ImageIcon(floorOneImg));
		
		buttonPanel = new JPanel();
		buttonPanel.add(okayButton);
		buttonPanel.add(previousButton);
		buttonPanel.add(nextButton);

		// Add components.
		this.add(cameraLabel, "span, wrap");
		this.add(imageLabel, "span, wrap");
		this.add(buttonPanel, "span, center");
		
		this.pack();
		this.setModal(true);
		this.setResizable(false);
		
		okayButton.addActionListener(new OkayListener());
		nextButton.addActionListener(new RightArrowListener());
		previousButton.addActionListener(new LeftArrowListener());
	}
	
	/**
	 * Open the dialog.
	 */
	public void open()
	{
		this.setVisible(true);
	}
	
	private class OkayListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			// Close dialog.
			CameraViewDialog.this.setVisible(false);
		}
	}
	
	private class LeftArrowListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e) 
		{			
			// Set to previous image.
			imageLabel.setIcon(new ImageIcon(imageArray[0]));
			imageLabel.revalidate();
		}
	}
	
	private class RightArrowListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			// Set to next image.
			imageLabel.setIcon(new ImageIcon(imageArray[1]));
			imageLabel.revalidate();
		}
	}
}
